package zucc.lcj.android_finalwork;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import pl.com.salsoft.sqlitestudioremote.SQLiteStudioService;

public class LoginActivity extends AppCompatActivity {
    TextView register;
    Button login;
    EditText user_id,user_pwd;
    private MyDatabaseHelper dbHelper,dbHelper1;
    private boolean isHas = false;
    public static String global_login_user;
    public static int global_login_user_id;
    public static String global_login_user_wechat;
    public static String getGlobal_login_sex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        SQLiteStudioService.instance().start(this);

        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);
//        dbHelper1 = new MyDatabaseHelper(this,"BookStore.db",null,1);

        register = (TextView) findViewById(R.id.register);
        login = (Button) findViewById(R.id.loginBtn);
        user_id = (EditText) findViewById(R.id.user_id);
        user_pwd = (EditText) findViewById(R.id.user_pwd);

        login.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
//                SQLiteDatabase db1 = dbHelper1.getWritableDatabase();
                String id = String.valueOf(user_id.getText());
                String password = String.valueOf(user_pwd.getText());
                global_login_user = id;
                Cursor cursor = db.rawQuery("select * from UserInfo",null);
                if(cursor.moveToFirst()==false)
                    Toast.makeText(getApplicationContext(), "表中没有数据", Toast.LENGTH_SHORT).show();
                if(cursor.moveToFirst()){
                    do{
                        if(id.equals(((Cursor) cursor).getString(cursor.getColumnIndex("user_name"))) && password.equals(cursor.getString(cursor.getColumnIndex("user_pwd")))){
                            global_login_user_id = cursor.getInt(cursor.getColumnIndex("user_id"));
                            isHas = true;
                            break;
                        }
                    }while(cursor.moveToNext());
                }
                if(isHas==true){
                    System.out.println("username"+global_login_user);
                    Cursor cursor1 = db.rawQuery("select * from UserInfo where user_name=?",new String[]{global_login_user});
                    if (cursor1.moveToFirst()){
                        getGlobal_login_sex=cursor1.getString(cursor.getColumnIndex("user_sex"));
                    }
                    System.out.println("sex " + getGlobal_login_sex);
                    if (getGlobal_login_sex.equals("男")){
                        String sql="select * from WechatMen where wechatMen_user="+global_login_user_id;
                        Cursor cursor2 = db.rawQuery(sql,null);
                        if (cursor2.moveToFirst()){
                            global_login_user_wechat=cursor2.getString(cursor2.getColumnIndex("wechatMen_name"));
                        }
                    }

                    else if (getGlobal_login_sex.equals("女")){
                        String sql="select * from WechatWomen where wechatWomen_user="+global_login_user_id;
                        Cursor cursor2 = db.rawQuery(sql,null);
                        if (cursor2.moveToFirst()){
                            global_login_user_wechat=cursor2.getString(cursor2.getColumnIndex("wechatWomen_name"));
                        }
                    }
                    System.out.println("global " + global_login_user_wechat);
                    Intent intent = new Intent(LoginActivity.this, FriendsActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(getApplicationContext(), "用户名或密码输入错误", Toast.LENGTH_SHORT).show();
                }
            }

        });


        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

}